package com.pdstudio.carrecom.ui.activity.more;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.http.RequestParams;
import com.pdstudio.carrecom.R;
import com.pdstudio.carrecom.api.HttpExecuteJson;
import com.pdstudio.carrecom.api.HttpExecuteJson.httpReturnJson;
import com.pdstudio.carrecom.api.ServiceHelper;
import com.pdstudio.carrecom.app.AppContext;
import com.pdstudio.carrecom.app.UIHelper;
import com.pdstudio.carrecom.bean.LoginInfo;
import com.pdstudio.carrecom.bean.ResultInfo;
import com.pdstudio.carrecom.tools.StringUtils;
import com.pdstudio.carrecom.ui.activity.ActivityMain;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 修改密码 类: ActivityModifierPassword <br>
 * 描述: TODO <br>
 * 作者: xiangzhaoyu <br>
 * 时间: 2015-5-25 下午8:28:06
 */
public class ActivityModifierPassword extends Activity {

	private TextView txtTitle;
	private ImageView ivBack;
	private Button mSubmit;
	private TextView mName;
	private EditText mUsername, mOldPass, mNewPass, mNewPassAg;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_more_modifierpassword1);

		initTitle();
		InitialView();
	}

	private void initTitle() {
		txtTitle = (TextView) findViewById(R.id.title_text_nav);

		txtTitle.setText("修改密码");

		ivBack = (ImageView) findViewById(R.id.title_back);
		ivBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	private void InitialView() {
		// mName=(TextView)findViewById(R.id.more_mp_username);
		// mName.setText(AppContext.getInstance()._userName);
		mUsername = (EditText) findViewById(R.id.et_mp_username);
		mUsername.setText(AppContext.getInstance()._userName);
		mOldPass = (EditText) findViewById(R.id.et_mp_oldpass);
		mNewPass = (EditText) findViewById(R.id.et_mp_pass);
		mNewPassAg = (EditText) findViewById(R.id.et_mp_passag);

		mSubmit = (Button) findViewById(R.id.btn_mp_submit);
		mSubmit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (mUsername.getText() == null
						|| mUsername.getText().equals("")
						|| mOldPass.getText().toString().equals("")
						|| mNewPass.getText().toString().equals("")
						|| mNewPassAg.getText().toString().equals("")) {
					UIHelper.ToastMessage(ActivityModifierPassword.this,
							"抢确认填写所有项目，请重新登录");
				} else {
					if (mNewPassAg.getText().toString().equals(mNewPass.getText()
							.toString()) ) {
						try {
							HttpExecuteJson http = new HttpExecuteJson(
									ActivityModifierPassword.this,
									mLoginListener);
							RequestParams rps = new RequestParams();
							rps.addBodyParameter("name", mUsername.getText().toString());
							rps.addBodyParameter("oldpasswd",
									StringUtils.md5(mOldPass.getText().toString()));
							rps.addBodyParameter("newpasswd",
									StringUtils.md5(mNewPass.getText().toString()));
							http.post(ServiceHelper.ModifierPassSubmit, rps);
						} catch (Exception e) {
							e.printStackTrace();
						}


					} else {
						UIHelper.ToastMessage(ActivityModifierPassword.this,
								"两次新密码不一致");						
					}
				}
			}
		});
	}

	private void DoJson(String json) {
		ResultInfo edv = null;
		try {
			edv = new Gson().fromJson(json, new TypeToken<ResultInfo>() {
			}.getType());
			if ((edv != null)
					&& (edv.msg.equals(ServiceHelper.msg_code_sucess))) {
				UIHelper.ToastMessage(ActivityModifierPassword.this, "修改密码成功!");
				finish();
			} else {
				UIHelper.ToastMessage(ActivityModifierPassword.this, "修改密码失败!"+edv.msg);

			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private httpReturnJson mLoginListener = new httpReturnJson() {
		@Override
		public void onFailure(int error, String msg) {
			UIHelper.ToastMessage(ActivityModifierPassword.this, "登录失败" + msg);
		}

		@Override
		public void onCancel() {
			// TODO Auto-generated method stub
			UIHelper.ToastMessage(ActivityModifierPassword.this, "登录失败");
		}

		// 获取到饲料配方类型成功
		public void onSuccess(String result) {
			// TODO Auto-generated method stub
			// UIHelper.ToastMessage(_context,"总条数=" + result + "");
			DoJson(result);
		}
	};

}
